import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router'
import { getToken } from '../utils/token';
const Layout = () => import('@/layout/Layout.vue');
const SlideMenuLayout = () => import('@/layout/SlideMenuLayout.vue');
const UserAdminLayout = () => import('@/layout/UserAdminLayout.vue');
const EmptyLayout = () => import('@/layout/EmptyLayout.vue');

export const constantRouterMap = [
  {
    path: '/',
    component: Layout,
    name: 'Root',
    redirect: '/home',
    meta: {
      hidden: true
    },
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Home/home.vue'),
        meta: {
          title: '首页'
        },
      },
      
    ]
  },
  {
    path: '/aboutUs',
    name: 'About',
    component: SlideMenuLayout,
    redirect: '/aboutUs/briefIntroduction',
    meta: {
      hidden: true,
      title: '关于我们'
    },
    children: [
      {
        path: 'briefIntroduction',
        name: 'BriefIntroduction',
        component: () => import('@/views/About/briefIntroduction.vue'),
        meta: {
          title: '企业简介'
        },
      },
      {
        path: 'spiritualConcept',
        name: 'SpiritualConcept',
        component: () => import('@/views/About/spiritualConcept.vue'),
        meta: {
          title: '企业精神'
        },
      },
      {
        path: 'termsOfService',
        name: 'termsOfService',
        component: () => import('@/views/About/termsOfService.vue'),
        meta: {
          title: '服务条款'
        },
      },
    ]
  },
  {
    path: '/contactUs',
    name: 'ContactUs',
    component: SlideMenuLayout,
    redirect: '/contactUs/info',
    meta: {
      hidden: true,
      title: '联系我们'
    },
    children: [
      {
        path: 'info',
        name: 'info',
        component: () => import('@/views/ContactUs/info.vue'),
        meta: {
          title: '联系我们'
        },
      },
    ]
  },
  {
    path: '/product',
    name: 'Product',
    component: SlideMenuLayout,
    redirect: '/product/gameInfo',
    meta: {
      hidden: true,
      title: '产品信息'
    },
    children: [
      {
        path: 'gameInfo',
        name: 'GameInfo',
        component: () => import('@/views/Product/gameInfo.vue'),
        meta: {
          title: '四方游戏'
        },
      },
      {
        path: 'sportsGame',
        name: 'SportsGame',
        component: () => import('@/views/Product/sportsGame.vue'),
        meta: {
          title: '体彩游戏'
        },
      },
      {
        path: 'allGame',
        name: 'Allgame',
        component: () => import('@/views/Product/allgame.vue'),
        meta: {
          title: '游戏介绍'
        },
      },
      {
        path: 'colorBallGame',
        name: 'ColorBallGame',
        component: () => import('@/views/Product/colorBallGame.vue'),
        meta: {
          title: '彩球介绍'
        },
      },
    ]
  },
  {
    path: '/gameQuestions',
    name: 'GameQuestions',
    component: SlideMenuLayout,
    redirect: '/gameQuestions/frequentlyOnSports',
    meta: {
      hidden: true,
      title: '游戏问题'
    },
    children: [
      {
        path: 'frequentlyOnSports',
        name: 'FrequentlyOnSports',
        component: () => import('@/views/GameQuestions/frequentlyOnSports.vue'),
        meta: {
          title: '体育常见问题'
        },
      },
      {
        path: 'lotteryProblem',
        name: 'LotteryProblem',
        component: () => import('@/views/GameQuestions/lotteryProblem.vue'),
        meta: {
          title: '彩票问题'
        },
      },
      {
        path: 'eSports',
        name: 'ESports',
        component: () => import('@/views/GameQuestions/eSports.vue'),
        meta: {
          title: '电子竞技'
        },
      },
      {
        path: 'sportsProblem',
        name: 'SportsProblem',
        component: () => import('@/views/GameQuestions/sportsProblem.vue'),
        meta: {
          title: '体育问题'
        },
      },
      {
        path: 'realGame',
        name: 'RealGame',
        component: () => import('@/views/GameQuestions/realGame.vue'),
        meta: {
          title: '真人游戏'
        },
      },
      {
        path: 'otherProblems',
        name: 'OtherProblems',
        component: () => import('@/views/GameQuestions/otherProblems.vue'),
        meta: {
          title: '其他问题'
        },
      },
    ]
  },
  {
    path: '/gameDetails',
    name: 'GameDetails',
    component: Layout,
    redirect: '/gameDetails/frequentlyOnSports',
    meta: {
      hidden: true,
      title: '游戏详情'
    },
    children: [
      {
        path: 'casino',
        name: 'Casino',
        component: () => import('@/views/GameDetails/Casino.vue'),
        meta: {
          title: '真人娱乐'
        },
      },
      {
        path: 'sport',
        name: 'Sport',
        component: () => import('@/views/GameDetails/Sport.vue'),
        meta: {
          title: '体育赛事'
        },
      },
      {
        path: 'fishHunting',
        name: 'FishHunting',
        component: () => import('@/views/GameDetails/FishHunting.vue'),
        meta: {
          title: '捕鱼游戏'
        },
      },
      {
        path: 'chess',
        name: 'Chess',
        component: () => import('@/views/GameDetails/Chess.vue'),
        meta: {
          title: '棋牌游戏'
        },
      },
      {
        path: 'slotMachine',
        name: 'SlotMachine',
        component: () => import('@/views/GameDetails/SlotMachine.vue'),
        meta: {
          title: '电子游艺'
        },
      },
      {
        path: 'lottery',
        name: 'Lottery',
        component: () => import('@/views/GameDetails/Lottery.vue'),
        meta: {
          title: '彩票游戏'
        },
      },
      {
        path: 'esport',
        name: 'Esport',
        component: () => import('@/views/GameDetails/Esport.vue'),
        meta: {
          title: '电竞游艺'
        },
      },
      {
        path: 'horse',
        name: 'Horse',
        component: () => import('@/views/GameDetails/Horse.vue'),
        meta: {
          title: '赛马游戏'
        },
      },
    ]
  },
  {
    path: '/admin',
    name: 'Admin',
    component: UserAdminLayout,
    redirect: '/admin/gamingWallet',
    meta: {
      hidden: true,
      requiresAuth: true,
    },
    children: [
      {
        path: 'gamingWallet',
        name: 'GamingWallet',
        component: () => import('@/views/Admin/gamingWallet.vue'),
        meta: {
          title: '游戏钱包',
          admin: true,
        },
      },
      {
        path: 'bettingRecord',
        name: 'BettingRecord',
        component: () => import('@/views/Admin/bettingRecord.vue'),
        meta: {
          title: '投注记录',
          admin: true,
        },
      },
      {
        path: 'fundRecord',
        name: 'FundRecord',
        component: () => import('@/views/Admin/fundRecord.vue'),
        meta: {
          title: '资金记录',
          admin: true,
        },
      },
      {
        path: 'depositArea',
        name: 'DepositArea',
        component: () => import('@/views/Admin/depositArea.vue'),
        meta: {
          title: '存款专区',
          admin: true,
        },
      },
      {
        path: 'withdrawalArea',
        name: 'WithdrawalArea',
        component: () => import('@/views/Admin/withdrawalArea.vue'),
        meta: {
          title: '取款专区',
          admin: true,
        },
      },
      {
        path: 'brankCard',
        name: 'brankCard',
        component: () => import('@/views/Admin/brankCard.vue'),
        meta: {
          title: '银行账户',
          admin: true,
        },
      },
      {
        path: 'siteLetter',
        name: 'SiteLetter',
        component: () => import('@/views/Admin/siteLetter.vue'),
        meta: {
          title: '站内信',
          admin: true,
        },
      },
      {
        path: 'personalInformation',
        name: 'PersonalInformation',
        component: () => import('@/views/Admin/personalInformation.vue'),
        meta: {
          title: '个人资料',
          admin: true,
        },
      },
      {
        path: 'vipDetails',
        name: 'VipDetails',
        component: () => import('@/views/Admin/vipDetails.vue'),
        meta: {
          title: 'VIP详情',
          admin: true,
        },
      },
      {
        path: 'recommendationPlan',
        name: 'RecommendationPlan',
        component: () => import('@/views/Admin/recommendationPlan.vue'),
        meta: {
          title: '推荐计划',
          admin: true,
        },
      },
      {
        path: 'loginPassword',
        name: 'LoginPassword',
        component: () => import('@/views/Admin/loginPassword.vue'),
        meta: {
          title: '登入密码',
          admin: true,
        },
      },
      {
        path: 'withdrawalsPassword',
        name: 'WithdrawalsPassword',
        component: () => import('@/views/Admin/withdrawalsPassword.vue'),
        meta: {
          title: '取款密码',
          admin: true,
        },
      },
    ]
  },
  {
    path: '/redirect',
    component: EmptyLayout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      },
      {
        path: '/callback',
        name: 'Callback',
        component: () => import('@/views/GameRedirect/Callback.vue'),
        meta: {
          title: 'loading',
          requiresAuth: true,
        }
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: '登录',
      noTagsView: true
    }
  },
  {
    path: '/forgot-password',
    component: () => import('@/views/ForgotPassword/ForgotPassword.vue'),
    name: 'ForgotPassword',
    meta: {
      hidden: true,
      title: '忘记密码',
      noTagsView: true
    }
  },
  {
    // 邀请注册
    path: '/integration/ashare',
    component: () => import('@/views/Register/Register.vue'),
    name: 'Integration',
    meta: {
      hidden: true,
      title: '注册',
      noTagsView: true
    }
  },
  {
    path: '/register',
    component: () => import('@/views/Register/Register.vue'),
    name: 'Register',
    meta: {
      hidden: true,
      title: '注册',
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !isLoggedIn()) {
    // 如果需要登录才能访问的路由，且未登录，则跳转到首页
    next('/')
  } else {
    next()
  }
})


function isLoggedIn() {
  // 判断用户是否已经登录，这里可以根据自己的业务逻辑实现
  // 如果已经登录，返回 true，否则返回 false
  // 例如，如果用户已经登录，可以在本地存储中查找登录状态信息
  return !!getToken();
}

export default router
